<?php

class Router_Route_Abstract
{

    protected $_check = '';

    protected $_build = '';

    protected $_map = '';

    protected $_defs = array();

    protected $_reqs = array();

    public function __construct($params)
    {
        $this -> _check = $params['check'];
        $this -> _build = $params['build'];
        $this -> _defs = $params['defs'];
        $this -> _reqs = $params['reqs'];
        $this -> _map = $params['map'];
    }

    public function mapActionId($vals)
    {
        return str_replace(array_map('router_map_key', array_keys($vals)), array_values($vals), $this -> _map);
    }

    /**
     * @param string $actionId
     * @return bool
     */
    static public function isDispatchable($actionId)
    {
        
        $arr = explode(':', $actionId);
        
        $class = implode('_', array_map('ucfirst', explode('.', str_replace('@', '.controller.', $arr[0]))));

        if (class_exists($class, 0))
        {
            return 1;

        }

        if (file_exists($file = MODULE_PATH . '/' . strtolower(str_replace('_', '/', $class)) . '.php'))
        {
            require_once $file;
            if (class_exists($class, 0))
            {
                return 1;
            }
        }

        return 0;
    }

}

function router_map_key($name)
{
    return '[' . $name . ']';
}
